import 'package:flutter/material.dart';
import 'package:get/get.dart';

class CustomPaper extends StatefulWidget {
  const CustomPaper({super.key});

  @override
  _CustomPaperState createState() => _CustomPaperState();
}

class _CustomPaperState extends State<CustomPaper> {
  late final ThemeData _themeData = Theme.of(context);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        // appBar: AppBar(title: Text("自定义custom"),),
        body: SafeArea(
      child: Container(
        child: CustomPaint(painter: ShaderPainter()),
      ),
    ));
  }
}

//  画笔 着色器 shader
class ShaderPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    Paint paint = Paint()
      ..strokeWidth = 20
      ..color = Colors.green
      ..invertColors = true;

    canvas.drawLine(Offset(20, 200), Offset(300, 200), paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }
}

//  斜接限制strokeMiterLimit
//strokeMiterLimit只适用于【StrokeJoin.miter】。
// 它是一个对斜接的限定，如果超过阈值，会变成【StrokeJoin.bevel】。
// 因为考虑到锐角太尖，会影响视觉。
class StrokeMiterLimit extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint();
    Path path = Path();
    paint
      ..style = PaintingStyle.stroke
      ..color = Colors.blue
      ..strokeJoin = StrokeJoin.miter
      ..strokeWidth = 20;
    for (int i = 0; i < 4; i++) {
      path.reset();
      path.moveTo(50 + 150.0 * i, 50);
      path.lineTo(50 + 150.0 * i, 150);
      path.relativeLineTo(100, -(40.0 * i + 20));
      canvas.drawPath(path, paint..strokeMiterLimit = 2);
    }
    for (int i = 0; i < 4; i++) {
      path.reset();
      path.moveTo(50 + 150.0 * i, 50 + 150.0);
      path.lineTo(50 + 150.0 * i, 150 + 150.0);
      path.relativeLineTo(100, -(40.0 * i + 20));
      canvas.drawPath(path, paint..strokeMiterLimit = 3);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }
}

// stroke Join 线接类型, 线接类型只适用于Path的线段绘制。它不适用于用【Canvas.drawPoints】绘制的线。
class StrokeJoinPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    final Paint paint = Paint();
    paint
      ..style = PaintingStyle.stroke
      ..color = Colors.blue
      ..strokeWidth = 20;
    Path path = Path();
    //StrokeJoin.bevel - 斜角、
    // StrokeJoin.miter - 锐角、
    // StrokeJoin.round - 圆角
    path
      ..moveTo(50, 50)
      ..lineTo(50, 150)
      ..relativeLineTo(100, -50)
      ..relativeLineTo(0, 100);
    canvas.drawPath(path, paint..strokeJoin = StrokeJoin.bevel);

    path.reset();

    path
      ..moveTo(50 + 150, 50)
      ..lineTo(50 + 150, 150)
      ..relativeLineTo(100, -50)
      ..relativeLineTo(0, 100);
    canvas.drawPath(path, paint..strokeJoin = StrokeJoin.miter);
    path.reset();

    path
      ..moveTo(50 + 150 * 2, 50)
      ..lineTo(50 + 150 * 2, 150)
      ..relativeLineTo(100, -50)
      ..relativeLineTo(0, 100);
    canvas.drawPath(path, paint..strokeJoin = StrokeJoin.round);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }
}

// stroke cap 线帽类型
class StrokeCapPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    final Paint paint = Paint();
    paint
      ..color = Colors.blue
      ..strokeWidth = 20;
    canvas.drawLine(
        Offset(50, 50), Offset(50, 150), paint..strokeCap = StrokeCap.butt);
    canvas.drawLine(Offset(50 + 50, 50), Offset(50 + 50, 150),
        paint..strokeCap = StrokeCap.round);
    canvas.drawLine(Offset(50 + 50 * 2, 50), Offset(50 + 50 * 2, 150),
        paint..strokeCap = StrokeCap.square);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }
}

class PaintStylePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    final Paint paint = Paint();
    paint..color = Colors.blue;
    canvas.drawCircle(
        Offset(150, 150),
        140,
        paint
          ..style = PaintingStyle.stroke
          ..strokeWidth = 20);
    paint..color = Colors.blue;
    canvas.drawCircle(
        Offset(150 + 360, 150),
        150,
        paint
          ..color = Colors.red
          ..isAntiAlias = false
          ..style = PaintingStyle.fill);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }
}

class PaperPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    final Paint paint = Paint();
    canvas.drawCircle(Offset(100, 100), 10, paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }
}

class LinePaper extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    final Paint paint = Paint();
    paint
      ..color = Colors.blue
      ..strokeWidth = 4
      ..style = PaintingStyle.stroke;
    canvas.drawLine(Offset(0, 0), Offset(100, 100), paint);
    canvas.drawLine(Offset(0, 0), Offset(100, 100), paint);

    Path path = Path();

    path.moveTo(100, 100);
    path.lineTo(200, 0);
    canvas.drawPath(path, paint..color = Colors.red);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }
}

class CirclePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    // TODO: implement paint
    final Paint paint = Paint();
    paint..color = Colors.blue;
    canvas.drawCircle(Offset(150, 150), 150, paint);
    paint..color = Colors.blue;
    canvas.drawCircle(
        Offset(150 + 360, 150),
        150,
        paint
          ..color = Colors.red
          ..isAntiAlias = false);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    // TODO: implement shouldRepaint
    return false;
  }
}
